//
//  DisplayAlertViewController.m
//  ALittleReminder
//
//  Created by Lion User on 23/11/2011.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "DisplayAlertViewController.h"
#import "SetAlertViewController.h"
#import "SetTimeViewController.h"
#import "AppDelegate.h"


@implementation DisplayAlertViewController{
    AlertManagedObject * myAlert;
    AVAudioPlayer *audioPlayer;
}
@synthesize nameTextField;
@synthesize timeButton;
@synthesize noteTextView;
@synthesize imageView;
- (id)initWithAlert:(AlertManagedObject *)alert
{
    self = [super initWithNibName:@"DisplayAlertViewController" bundle:nil];
    if (self) {
        myAlert = alert;
    }
    return self;
}

//do not use
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
- (void)viewWillAppear:(BOOL)animated
{
    if (myAlert) {
        //show the name if exists
        if (myAlert.name) {
            nameTextField.text = myAlert.name;
        }else{
            nameTextField.text = @"";
        }
        //show the date if exists
        if (myAlert.date) {
            NSString *time = [myAlert timeToString];
            
            [timeButton setTitle:time forState:UIControlStateNormal];
        }else{
            [timeButton setTitle:@"???" forState:UIControlStateNormal];
        }
        //show the image if exists
        [myAlert useImage:^(UIImage * image)
         {
             //NSLog(@"use image:%@",image);
             [imageView setImage:image];
         }];
        //Show the text if exits
        noteTextView.text = myAlert.text;
        //play sound if exits
        if(myAlert.audioFileName){
            AVAudioSession *audioSession = [AVAudioSession sharedInstance];
            [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
            
            audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:myAlert.audioFileName] error:nil];
            if (audioPlayer) {
                audioPlayer.numberOfLoops = 2;
                [audioPlayer play];
            }
        }
    }
    [super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [AppDelegate saveManagedObjects];
    if(audioPlayer){
        [audioPlayer stop];
    }
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setNameTextField:nil];
    [self setTimeButton:nil];
    [self setNoteTextView:nil];
    [self setImageView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - user actions

- (IBAction)onOpenTime:(id)sender {
    [self textFieldShouldReturn:nameTextField];//hide the keyboard
    SetTimeViewController *openView = [[SetTimeViewController alloc] initWithAlert:myAlert];
    [self presentModalViewController:openView animated:YES];
}

- (IBAction)onOk:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)onEdit:(id)sender {
    SetAlertViewController *openView = [[SetAlertViewController alloc] initWithAlert:myAlert];
    UIViewController *parent = [self parentViewController];
    if(!parent) parent = [self presentingViewController];// ios 5 support
    [self dismissModalViewControllerAnimated:NO];
    [parent presentModalViewController:openView animated:NO];
}

- (BOOL) textFieldShouldReturn:(UITextField*) textField {
    myAlert.name = nameTextField.text;
    [textField resignFirstResponder];
    return YES;
}

@end
